package com.xh.demo.config.custom;

import com.xh.demo.common.annotation.MyRequestBody;
import com.xh.demo.domain.vo.UserVo;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import java.util.Date;

/**
 * @Name UserVoArgumentResolver
 * @Description 自定义参数转换器(对@RequestBody 绑定的参数无效)
 * @Author wen
 * @Date 2019-08-02
 */
public class UserVoArgumentResolver implements HandlerMethodArgumentResolver {

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        boolean flag = UserVo.class.isAssignableFrom(parameter.getParameterType());
        return (!parameter.hasParameterAnnotation(MyRequestBody.class) && flag);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        //TODO Add BusinessException Logic
//        HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
//        HttpServletResponse response = nativeWebRequest.getNativeRequest(HttpServletResponse.class);
        return new UserVo().setCreateTime(new Date()).setId(111L).setName("张三").setSex(1);
    }
}
